#include "main.h"

static volatile DWORD doLoop = true;

void DisplayProcesses( void ) {
	printf( "%s\n", "Displaying processes" );
	PROCESSENTRY32 processEntry;
	HANDLE hSnapshot = CreateToolhelp32Snapshot ( TH32CS_SNAPPROCESS, NULL );
	processEntry.dwSize = sizeof(processEntry);

	if ( hSnapshot && Process32First( hSnapshot, &processEntry ) ) {
		printf( "%s\n", processEntry.szExeFile );

		while ( Process32Next( hSnapshot, &processEntry ) ) {
			printf( "%-15s%d\n", processEntry.szExeFile, processEntry.th32ProcessID );
		}
	}
	printf( "%s\n", "Done displaying processes" );
}

void _declspec(naked) MoreStuff( void ) {
	__asm XOR EAX,EAX
	__asm RET
}

class Klass {
private:
	int val;
public:
	Klass( int a ) {
		val = a;
	}

	~Klass( ) {
		val = 0;
	}
};

static volatile DWORD saveVal = 0;
static Klass k = Klass( 2 );
static DWORD testVal = 0;

void __declspec(naked) DoStuff( void ) {
    //__asm MOV saveVal, EAX
	//__asm MOV EAX, DWORD PTR DS:[ESP+4]
	__asm MOV EAX, 50
	__asm CALL MoreStuff
	__asm MOV saveVal, EAX
	//printf("%s=%d", "bar", saveVal);
	//saveVal = strcmp("ab","cd");
	__asm MOV EAX, saveVal
	__asm MOV doLoop, EAX
	__asm MOV doLoop, ESP
	__asm RET
}



int main( size_t argc, char ** argv ) {
	//DisplayProcesses( );

	printf( "DoStuff located 0x%08X\n", DoStuff );
	printf( "doLoop located 0x%08X\n", &doLoop );
	
	system("PAUSE");
	 
	DoStuff( );

	while ( doLoop ) {

	}

	printf( "doLoop val: %d\n", doLoop );

	system("PAUSE");
	return 0;
}